package com.example.liturgiagodzin;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SecondActivity extends ActionBarActivity {

    private ArrayList<String> listNames = new ArrayList<String>();
    private ArrayList<String> listLinks = new ArrayList<String>();
    private ArrayList<String> choosenLinksList = new ArrayList<String>();
    private ArrayList<String> choosenPrayersList = new ArrayList<String>();

    private String choosenLink = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        setContentView(R.layout.activity_second);
        Intent sender = getIntent();
        choosenLink = sender.getExtras().getString("choosenLink");
        TextView textView = (TextView)findViewById(R.id.textView);
        textView.setText(PrepareContent.getInstance().getDislayDate());

//        ListView listview = (ListView) findViewById(R.id.listView);
        Button next = (Button)findViewById(R.id.button);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(),MainActivity.class);
                startActivityForResult(intent, RESULT_OK);
                finish();

            }
        });

        choosenLink = choosenLink.substring(0,choosenLink.lastIndexOf("/")+1);
        preparePrayersList();

        ListView listView = (ListView)findViewById(R.id.listView);
        listView.setCacheColorHint(Color.TRANSPARENT);
        StableArrayAdapter adapter = adapter = new StableArrayAdapter(getBaseContext(),
                R.layout.prayerlist, R.id.textItem, choosenPrayersList){

            @Override
            public View getView(int position, View convertView,
                                ViewGroup parent) {
                View view =super.getView(position, convertView, parent);

                TextView textView=(TextView) view.findViewById(R.id.textItem);//(android.R.id.text1);
                textView.setTextColor(Color.WHITE);
                textView.setGravity(Gravity.CENTER);

                return view;
            }
        };

        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                                    int position, long id) {
                final String item = (String) parent.getItemAtPosition(position);
                //Toast.makeText(getApplicationContext(), "Wciśnieto: " + listLinks.get(position), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(view.getContext(), ThirdActivity.class);
                intent.putExtra("choosenLink", choosenLinksList.get(position));
                intent.putExtra("choosenPrayer", choosenPrayersList.get(position));
                //startActivity(intent);

                startActivityForResult(intent, Activity.RESULT_OK);
                finish();
            }

        });


    }

    private void preparePrayersList(){
        String[] choosenLinksTab = new String[]{
                choosenLink + "wezw.php3",
                choosenLink + "jutrznia.php3",
                choosenLink + "godzczyt.php3",
                choosenLink + "modlitwa1.php3",
                choosenLink + "modlitwa2.php3",
                choosenLink + "modlitwa3.php3",
                choosenLink + "nieszpory.php3",
                choosenLink + "kompleta.php3"
        };
        for(int i = 0; i<choosenLinksTab.length; i++){
            choosenLinksList.add(choosenLinksTab[i]);
        }
        String[] choosenPrayers = new String[]{
                "Wezwanie",
                "Jutrznia",
                "Godzina czytań",
                "Modlitwa przedpołudniowa",
                "Modlitwa południowa",
                "Modlitwa popołudniowa",
                "Nieszpory",
                "Kompleta"
        };
        for(int i = 0; i<choosenPrayers.length; i++){
            choosenPrayersList.add(choosenPrayers[i]);
        }

    }

//    public void onBtnClick(View view){
//        Intent intent = new Intent(view.getContext(), ThirdActivity.class);
//        switch (view.getId()){
//            case R.id.btnWezwanie:
//                intent.putExtra("choosenLink",choosenLink + "wezw.php3");
//                intent.putExtra("choosenPrayer", "Wezwanie");
//                startActivityForResult(intent, 0);
//                break;
//            case R.id.btnJutrznia:
//                intent.putExtra("choosenLink",choosenLink + "jutrznia.php3");
//                intent.putExtra("choosenPrayer", "Jutrznia");
//                startActivityForResult(intent, 0);
//                break;
//            case R.id.btnGodz:
//                intent.putExtra("choosenLink",choosenLink + "godzczyt.php3");
//                intent.putExtra("choosenPrayer", "Godzina czytań");
//                startActivityForResult(intent, 0);
//                break;
//            case R.id.btnMPrzedPol:
//                intent.putExtra("choosenLink",choosenLink + "modlitwa1.php3");
//                intent.putExtra("choosenPrayer", "Modlitwa przedpołudniowa");
//                startActivityForResult(intent, 0);
//                break;
//            case R.id.btnMPol:
//                intent.putExtra("choosenLink",choosenLink + "modlitwa2.php3");
//                intent.putExtra("choosenPrayer", "Modlitwa południowa");
//                startActivityForResult(intent, 0);
//                break;
//            case R.id.btnMPoPol:
//                intent.putExtra("choosenLink",choosenLink + "modlitwa3.php3");
//                intent.putExtra("choosenPrayer", "Modlitwa popołudniowa");
//                startActivityForResult(intent, 0);
//                break;
//            case R.id.btnNiesz:
//                intent.putExtra("choosenLink",choosenLink + "nieszpory.php3");
//                intent.putExtra("choosenPrayer", "Nieszpory");
//                startActivityForResult(intent, 0);
//                break;
//            case R.id.btnKomp:
//                intent.putExtra("choosenLink",choosenLink + "kompleta.php3");
//                intent.putExtra("choosenPrayer", "Kompleta");
//                startActivityForResult(intent, 0);
//                break;
//        }
//        finish();
//    }


    private class StableArrayAdapter extends ArrayAdapter<String> {

        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public StableArrayAdapter(Context context, int layoutId, int textViewResourceId,
                                  List<String> objects) {
            super(context, layoutId, textViewResourceId, objects);
            for (int i = 0; i < objects.size(); ++i) {
                mIdMap.put(objects.get(i), i);
            }
        }

        @Override
        public long getItemId(int position) {
            String item = getItem(position);
            return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        Intent intent = new Intent(getBaseContext() ,MainActivity.class);
        startActivityForResult(intent, RESULT_OK);
        finish();
    }

//    @Override
//    protected void onStop() {
//        super.onStop();
//        finish();
//    }
}
